package com.jf.cloud.service.sysmanager.common.util.qnImg;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.jf.cloud.service.sysmanager.common.util.DateUtil;
import com.jf.cloud.service.sysmanager.common.util.FileUtil;
import com.jf.cloud.service.sysmanager.common.util.MD5;
import com.jf.cloud.service.sysmanager.common.util.StringUtil;
import com.jf.cloud.service.sysmanager.constant.ImagePath;
import com.jf.cloud.service.sysmanager.support.entity.PicFile;

public class UplodImg {

	private static Logger log = LoggerFactory.getLogger(UplodImg.class);
	public static final String bucketName = "i3yx";
	
	/**
	 * 按类型上传图片 
	 * @param type:上传图片的类型 [不同的类型需要不同的路径] 
	 * @param fileName:上传文件的名称（input的name属性）默认为 Filedata
	 * @return 类型|图片上传的保存的地址 
	 * wrongtype:文件格式不正确 
	 * bigger:文件过大 
	 * @throws IOException
	 */
	public static String up(HttpServletRequest request, String type, String fileName, HttpServletResponse response) throws IOException {
		String returnPathString = "error";
		fileName = fileName != null && fileName.length() > 0 ? fileName : "Filedata";
		log.info("上传图片,type:" + type + " fileName:" + fileName);
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile file = multipartRequest.getFile(fileName);
		response.setCharacterEncoding("GBK");
		String namekey = "";
		if (file.getSize() == 0) {
			return "";
		}
		
		if (file.getSize() > 5120000) {
			log.info("图片文件太大不能上传");
			request.setAttribute("msg", "图片文件太大不能上传");
			return "bigger";
		}
		
		String filename = "", fileSuffix = ""; // 文件名称，后缀
		PicFile pfile = new PicFile();
		boolean rs = true;
		String origianlFileName = file.getOriginalFilename();
		if (StringUtil.isNotEmpty(origianlFileName) && !StringUtil.isImg(origianlFileName)) {
			return "isNotImg";
		}
		namekey = MD5.getMessageDigest(origianlFileName.getBytes()).substring(0, 5);
		if (!origianlFileName.contains(".")) { // 乱码的文件名称 需要处理
			if (origianlFileName.length() > 4) {
				String subName = origianlFileName.substring(origianlFileName.length() - 4, origianlFileName.length());
				origianlFileName = "default." + FileUtil.getSuffix(subName);
			} else { // 强制默认一个图片名称
				//origianlFileName = "default.jpg";
				return null;
			}
		}
		if (file != null && origianlFileName.length() > 2 && origianlFileName.contains(".")) { // 存在文件
			filename = origianlFileName;
			fileSuffix = filename.substring(filename.lastIndexOf("."), filename.length());
			if (!FileUtil.checkFileSuffix(fileSuffix)) {
				returnPathString = "wrongtype";
			} else {
				pfile.setContent(file.getBytes());
				String dateName = type + DateUtil.getCurrentDateTime("yyyyMMddHHmmssSSS");
				pfile.setFileName(dateName + namekey + fileSuffix);
				String path = ImagePath.getPath(type);
				pfile.setFilePath(path);
				rs = ImgCloud.up(pfile.getFilePath() + pfile.getFileName(), new ByteArrayInputStream(pfile.getContent()));
				if (rs) {
					returnPathString = ImagePath.FOCUS_PATH + pfile.getFilePath() + pfile.getFileName();
				}
			}
		} else {
			log.info(" 未知图片名称 或 图片名称 乱码 ");
		}
		return returnPathString;
	}
	
	/**
	 * ※※※   上传图片至 云服务器   ※※※ 
	 * fileFullName:文件名称 包含后缀和相对目录  (域名+这个地址可以访问到的地址) /3yx/other/xxxx.jpg
	 * needUpImgTempPath:需要上传的图片的存放路径 （服务器临时路径也可以） c://temp/xxx.jpg
	 * bucketName :云服务器空间 一般不需要这个参数
	 * @return
	 */
	public static boolean up(PicFile pfile){
		return ImgCloud.up(pfile.getFilePath() + pfile.getFileName(), new ByteArrayInputStream(pfile.getContent()));
	}
}
